#!/bin/sh

# This script bootstraps autotools: it copies any necessary files,
# and generates the configure script, when checked out from git
# Copyright (C) Oliver Galvin 2018
# See COPYING for license information

#Check for dependencies
deps="automake autoconf autopoint pkgconf"
for dep in $deps; do
	$dep --version >/dev/null 2>&1 || (
		echo "Error: $dep is missing. Aborting."
		exit 1
	)
done

#Make sure we are in the same directory as the script
srcdir=$(dirname "$0")
cd "$srcdir" || exit 1

#Check the directory has the necessary template files
if ! test -f configure.ac || ! test -f makefile.am; then
	echo "Directory $srcdir does not look like a project root. Aborting."
	exit 1
fi

#shellcheck disable=SC2016
prog=$(autoconf -t 'AC_INIT:$1')
echo "$0: Bootstrapping $prog source directory..."

autopoint || exit 1
aclocal || exit 1
autoreconf -fi || exit 1

echo "...done. $prog can now be built and installed."
test -f INSTALL && echo "See the INSTALL file."
